package com.yy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.common.result.PageResult;
import com.yy.common.result.Result;
import com.yy.entity.*;
import com.yy.query.RegisterQuery;
import com.yy.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author Yang
 * @Create 2024/6/15 8:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/payment")
@Tag(name = "住院管理.缴费管理")
public class PaymentController {
    //入院
    private final ReportService reportService;
    //住院表
    private final RegisterService registerService;
    //科室
    private final DepartmentsService departmentsService;
    //医生
    private final DoctorService doctorService;
    //床位
    private final BedService bedService;
    //预交表
    private final PayService payService;
    //药房表
    private final PharmacyService pharmacyService;
    //住院收费表
    private final HospitalpriceService hospitalpriceService;
    //住院项目收费表
    private final InoutpatienttypeService inoutpatienttypeService;

    @GetMapping("/listPay")
    @Operation(summary = "缴费管理分页查询")
    public PageResult<Pay> listPay(int pageNum, int pageSize, Integer Hospitalizationnumber) {
        QueryWrapper<Pay> wrapper = new QueryWrapper<>();
        wrapper.eq("registerid", Hospitalizationnumber);
        Page<Pay> page_ = new Page<Pay>(pageNum, pageSize);
        return PageResult.success(payService.page(page_, wrapper));
    }

    @PostMapping("/addmoney/{Hospitalizationnumber}")
    @Operation(summary = "预交金额")
    public Result addmoney(@RequestBody JiaofeiPay jiaofeiPay, @PathVariable int Hospitalizationnumber) {
        Pay pay = new Pay();
        BeanUtils.copyProperties(pay, jiaofeiPay);
        pay.setRegisterid(Hospitalizationnumber);
        pay.setMoney(jiaofeiPay.getYujiao());
        pay.setPaydate(new Date());
        boolean save = payService.save(pay);
        return Result.judge(save);
    }

    @PutMapping("/updateregistermoney/{Hospitalizationnumber}/{money}")
    @Operation(summary = "预交金额时修改住院表的余额")
    public Result updateregistermoney(
            @PathVariable int Hospitalizationnumber,
            @PathVariable Double money) {
        boolean f = registerService.update(new LambdaUpdateWrapper<Register>()
                .eq(Register::getRegisterid, Hospitalizationnumber)
                .set(Register::getMoney, money));
        return Result.success(f);
    }

    @GetMapping("/hospinquire/{registerid}")
    @Operation(summary = "根据住院号查询已买药品信息的状态")
    public Result hospinquire(@PathVariable int registerid) {
        QueryWrapper<Hospitalprice> wrapper = new QueryWrapper<>();
        wrapper.eq("registerid", registerid);
        wrapper.eq("state", 0);
        List<Hospitalprice> list = hospitalpriceService.list(wrapper);
        return Result.success(list);
    }

    @GetMapping("/listInout")
    @Operation(summary = "检查项目列表分页查询")
    public PageResult<Inoutpatienttype> listInout(RegisterQuery registerQuery) {
        IPage<Inoutpatienttype> page = inoutpatienttypeService.selectiout(registerQuery);
        return PageResult.success(page);
    }
}
